import { parse } from 'url';

// mock tableListDataSource
let tableListDataSource = [
  {
    callNo: "1",
    desc: "食物类",
    name: "牛奶",
    en: "f1",
    updatedAt: "2019-04-12T14:00:00.000Z",
  },
  {
    callNo: "2",
    desc: "食物类",
    name: "鸡蛋清",
    en: "f2",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "4",
    desc: "食物类",
    name: "鱼",
    en: "f3",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "1",
    desc: "食物类",
    name: "小麦",
    en: "f4",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "2",
    desc: "食物类",
    name: "花生",
    en: "f13",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "2",
    desc: "食物类",
    name: "芝麻",
    en: "f14",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "3",
    desc: "食物类",
    name: "食物混合fx1",
    en: "f10",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "3",
    desc: "食物类",
    name: "螃蟹",
    en: "f23",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "3",
    desc: "食物类",
    name: "虾",
    en: "f24",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "1",
    desc: "食物类",
    name: "黄豆",
    en: "fx1",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "2",
    desc: "吸入类",
    name: "户尘螨",
    en: "d1",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "2",
    desc: "吸入类",
    name: "粉尘螨",
    en: "d2",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "3",
    desc: "吸入类",
    name: "矮豚草",
    en: "w1",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "2",
    desc: "吸入类",
    name: "艾蒿",
    en: "w2",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "2",
    desc: "吸入类",
    name: "猪皮屑",
    en: "E1",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "2",
    desc: "吸入类",
    name: "狗皮屑",
    en: "ES",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "3",
    desc: "吸入类",
    name: "烟曲霉",
    en: "m3",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "3",
    desc: "吸入类",
    name: "链格孢",
    en: "m6",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "4",
    desc: "吸入类",
    name: "螳螂",
    en: "i6",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  
];

function getGuoMinYuan(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = parse(url, true).query;

  let dataSource = tableListDataSource;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
  }

  if (params.name) {
    dataSource = dataSource.filter(data => data.name.indexOf(params.name) > -1);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  return res.json(result);
}

function postGuoMinYuan(req, res, u, b) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const body = (b && b.body) || req.body;
  const { method, name, desc, key } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => key.indexOf(item.key) === -1);
      break;
    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        name: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        desc,
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 2,
        updatedAt: new Date(),
        createdAt: new Date(),
        progress: Math.ceil(Math.random() * 100),
      });
      break;
    case 'update':
      tableListDataSource = tableListDataSource.map(item => {
        if (item.key === key) {
          Object.assign(item, { desc, name });
          return item;
        }
        return item;
      });
      break;
    default:
      break;
  }

  return getGuoMinYuan(req, res, u);
}

export default {
  'GET /api/guominyuan': getGuoMinYuan,
  'POST /api/guominyuan': postGuoMinYuan,
};
